package edu.udel.mboch.towerdefense;

import java.util.ArrayList;

public class SingleHumanPlayer extends Player implements Actor
{
	private static final long serialVersionUID = -8315679862112618356L;
	private int selection_row;
	private int selection_col;
	private int sellRow;
	private int sellCol;
	private Tower selection_tower;
	private String selection_towerType;
	
	public SingleHumanPlayer(String name)
	{
		super(name);
		resetSelection();
	}
	
	private void resetSelection()
	{
		selection_row = -1;
		selection_col = -1;
		sellRow = -1;
		sellCol = -1;
		selection_towerType = null;
		selection_tower = null;
	}
	
	public SingleHumanPlayer(String name, int cash, int lives, int income, ArrayList<Tower> towers)
	{
		super(name, cash, income, lives, towers);
		resetSelection();
	}
	
	public TDMove getNextMove(TDState state)
	{
		if(selection_col >= 0 && selection_row >= 0 && selection_tower != null && cash>=selection_tower.getCost() && state.getGrid().getTowers()[selection_row][selection_col] == null && state.getGrid().getTerrain()[selection_row][selection_col].isBuildable())
		{
			PlayerBuildMove move = new PlayerBuildMove(selection_tower);
			resetSelection();
			towers.add(move.getTower());
			cash -= move.getTower().getCost();
			return move;
		}
		else if(sellRow >= 0 && sellCol >= 0 && state.getGrid().getTowers()[sellRow][sellCol]!=null)
		{
			Tower tower = state.getGrid().getTowers()[sellRow][sellCol];
			PlayerSellMove move = new PlayerSellMove(tower);
			resetSelection();
			towers.remove(tower);
			cash += tower.getCost()*.75;
			return move;
		}
		return null;
	}
	
	public void clearTower()
	{
		selection_towerType = null;
		selection_tower = null;
	}
	
	public String getSelectionTowerType()
	{
		return selection_towerType;
	}

	public int getHeuristicScore(TDState state)
	{
		return 0;
	}
	
	public void handleSelection(int row, int col)
	{
		selection_row = row;
		selection_col = col;
		if(selection_towerType != null)
		{
			if(selection_towerType.equals("Basic"))
				selection_tower = new BasicTower(new Position(col, row));
			else if(selection_towerType.equals("Heavy"))
				selection_tower = new HeavyTower(new Position(col, row));
			else if(selection_towerType.equals("Slow"))
				selection_tower = new SlowTower(new Position(col, row));
			else if(selection_towerType.equals("Splash"))
				selection_tower = new SplashTower(new Position(col, row));
		}
	}
	
	public void handleSell(int row, int col)
	{
		sellRow = row;
		sellCol = col;
	}
	
	public void handleSelection(String towerType)
	{
		if(towerType!=null)
		{
			if(cash>=Tower.BASIC_COST && towerType.equals("Basic"))
				selection_towerType = towerType;
			else if(cash>=Tower.HEAVY_COST && towerType.equals("Heavy"))
				selection_towerType = towerType;
			else if(cash>=Tower.SLOW_COST && towerType.equals("Slow"))
				selection_towerType = towerType;
			else if(cash>=Tower.SPLASH_COST && towerType.equals("Splash"))
				selection_towerType = towerType;
		}
		else
			selection_towerType = null;
	}
	
	public SingleHumanPlayer copy()
	{
		ArrayList<Tower> towersCopy = new ArrayList<Tower>();
		for(Tower t : towers)
			towersCopy.add(t.copy());
			
		return new SingleHumanPlayer(name, cash, lives, income, towersCopy);
	}
	
	public String getTowerType()
	{
		return selection_towerType;
	}
}
